import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/auth'
// import Login from '@/views/login'
// import Layout from '@/views/layout'
// import Home from '@/views/home'
// import Article from '@/views/article'
// import Publish from '@/views/publish'
const Login = () => import('@/views/login')
const Layout = () => import('@/views/layout')
const Home = () => import('@/views/home')
const Article = () => import('@/views/article')
const Publish = () => import('@/views/publish')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: '/article', component: Article },
      { path: '/publish', component: Publish }
    ]
  },
  { path: '/login', component: Login }
]

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，没有token，跳转登录页面
  if (to.path !== '/login' && !token) {
    next('/login')
  }
  // 其他情况放行
  next()
})

export default router
